/*******************************************************************************
 * Copyright (c) 2012, 2013 XML-Print Project.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     XML-Print Project - initial API and implementation
 ******************************************************************************/
package info.xmlprint.core.mapping;

import info.xmlprint.core.xml.PElement;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class ElementPathNode implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 127089972611143679L;

	public static void main(String args[]) {
		PElement pe = new PElement("div");
		ElementPathNode epn = new ElementPathNode(pe);
		epn.addAttribut("n");
		epn.addAttribut("type");
		epn.addValue("n", "1");
		epn.addValue("n", "2");
		epn.addValue("n", "3");
		epn.addValue("type", "chapter");
		epn.toPrint();

	}

	private Map<String, Set<String>> attrMap = new HashMap<String, Set<String>>();
	private Set<String> attrWithSelectAll = new HashSet<String>();
	private HashSet<ElementPathNode> childrens = new HashSet<ElementPathNode>();

	private PElement currentPElement;

	private ElementPath ePath;
	private ElementPathNode parent;

	public ElementPathNode(PElement pe) {
		setPElement(pe);
	}

	public void addAttribut(String attrName) {
		if (attrMap.get(attrName) != null)
			return;

		Set<String> set = new HashSet<String>();
		attrMap.put(attrName, set);
	}

	public void addAttrWithSelectAll(String attrName) {
		attrWithSelectAll.add(attrName);
	}

	public void addChildren(ElementPathNode pn) {
		childrens.add(pn);
		ElementPathNode parent = pn.getParent();
		if (parent == null || !parent.equals(this)) {
			if (parent != null)
				parent.removeChildren(pn);

			pn.setParent(this);
		}
	}

	public void addValue(String attrName, String val) {
		addAttribut(attrName);
		attrMap.get(attrName).add(val);
	}

	public String[] getAttribut() {
		return attrMap.keySet().toArray(new String[0]);
	}

	public String[] getAttributValue(String attrName) {
		return attrMap.get(attrName).toArray(new String[0]);
	}

	public ElementPathNode[] getChildrens() {
		return childrens.toArray(new ElementPathNode[0]);
	}

	public ElementPath getInElementPath() {
		return this.ePath;
	}

	public ElementPathNode getParent() {
		return parent;
	}

	public PElement getPElement() {
		return currentPElement;
	}

	public boolean hasChildren() {
		if (childrens.size() > 0)
			return true;
		return false;
	}

	public boolean hasValue(String attrName, String val) {
		if (attrMap.containsKey(attrName)
				&& attrMap.get(attrName).contains(val))
			return true;

		return false;
	}

	public boolean isSelectAll(String attrName) {
		return attrWithSelectAll.contains(attrName);
	}

	public void removeAttribut(String attrName) {
		attrMap.remove(attrName);
	}

	public void removeAttrWithSelectAll(String attrName) {
		attrWithSelectAll.remove(attrName);
	}

	public void removeChildren(ElementPathNode pn) {
		childrens.remove(pn);
	}

	public void removeValue(String attrName, String val) {
		attrMap.get(attrName).remove(val);
	}

	public void setInElementPath(ElementPath ep) {
		this.ePath = ep;
	}

	public void setParent(ElementPathNode p) {
		parent = p;
		if (p != null)
			parent.addChildren(this);
	}

	public void setPElement(PElement pe) {
		this.currentPElement = pe;
	}

	public void toPrint() {
		System.out.println("Attribut-Value:");
		for (String attr : this.getAttribut()) {
			for (String v : this.getAttributValue(attr)) {
				System.out.println(attr + "-" + v);
			}
		}
	}
}
